/************************************************
 * Author       :   XXY
 * Blog         :   https://www.cnblogs.com/jzyl
 * Gitee        :   https://gitee.com/xiaobaichong
 * Email        :   1047185209@QQ.com
 * FileName     :   FileUtils.cs
 * CreateData   :   2023/5/16 17:21:18
 * UnityVersion :   2021.3.20f1c1
 * Description  :   文件工具类
************************************************/

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using UnityEditor;
using UnityEngine;

namespace Main
{
    /// <summary>
    /// 文件工具类
    /// </summary>
    public class FileUtility
    {
        /// <summary>
        /// 压缩为Zip文件
        /// </summary>
        /// <param name="filePaths">要压缩的文件</param>
        /// <param name="zipPath">输出路径</param>
        public static async void ZipFiles(string[] filePaths, string zipPath)
        {
            await Task.Run(() =>
            {
                // 创建一个新的zip文件
                using (ZipArchive archive = ZipFile.Open(zipPath, ZipArchiveMode.Update))
                {
                    foreach (string filePath in filePaths)
                    {
                        // 向zip文件中添加一个新文件
                        archive.CreateEntryFromFile(filePath, Path.GetFileName(filePath));
                    }
                }
            });
        }
        /// <summary>
        /// 加压一个Zip文件
        /// </summary>
        /// <param name="zipPath"></param>
        /// <param name="outPath"></param>
        public static async void UnZipFiles(string zipPath, string outPath)
        {
            await Task.Run(() => ZipFile.ExtractToDirectory(zipPath, outPath));
        }
        /// <summary>
        /// 打开一个目录
        /// </summary>
        /// <param name="path"></param>
        public static void OpenExplorer(string path)
        {
            path = Path.GetFullPath(path);
            Process.Start("explorer.exe", path);
        }
        /// <summary>
        /// 获取文件MD5
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static string GetFileMd5(string filePath)
        {
            using (MD5 md5 = MD5.Create())
            {
                // 打开文件流
                using (FileStream stream = File.OpenRead(filePath))
                {
                    // 计算文件的哈希值
                    byte[] hash = md5.ComputeHash(stream);

                    // 将哈希值转换为16进制字符串
                    string hashString = BitConverter.ToString(hash).Replace("-", "").ToLower();

                    return hashString;
                }
            }
        }
        /// <summary>
        /// 添加文件到Zip文件中
        /// </summary>
        /// <param name="zipPath">Zip文件路径</param>
        /// <param name="filePath">要加入压缩的文件路径</param>
        public static void AddFileToZip(string zipPath, string filePath)
        {
            using (FileStream zipStream = new FileStream(zipPath, FileMode.Create))
            {
                using (ZipArchive zip = new ZipArchive(zipStream, ZipArchiveMode.Create))
                {
                    // 将文件添加到 ZIP 文件中
                    zip.CreateEntryFromFile(filePath, Path.GetFileName(filePath));
                }
            }
        }
        /// <summary>
        /// 在Zip文件中创建一个文件并写入数据
        /// </summary>
        /// <param name="zipPath"></param>
        /// <param name="fileName"></param>
        /// <param name="data"></param>
        public static void CreateFileToZip(string zipPath,string fileName,string data)
        {
            using (ZipArchive archive = ZipFile.Open(zipPath, ZipArchiveMode.Update))
            {
                ZipArchiveEntry entry = archive.CreateEntry(fileName);

                using (StreamWriter writer = new StreamWriter(entry.Open()))
                {
                    writer.Write(data);
                }
            }
        }
        /// <summary>
        /// 创建一个zip文件
        /// </summary>
        /// <param name="zipPath"></param>
        public static string CreateZipFile(string zipPath)
        {
            using (FileStream zipStream = new FileStream(zipPath, FileMode.Create))
            {
                using (ZipArchive zip = new ZipArchive(zipStream, ZipArchiveMode.Create))
                {
                    return zipPath;                }
            }
        }
    }
}
